package com.czy.workflow.demo;

import cn.hutool.json.JSONUtil;
import com.czy.workflow.core.annotation.TaskExecutor;
import com.czy.workflow.core.annotation.TaskExecutorMethod;
import com.czy.workflow.core.context.FlowContext;
import com.czy.workflow.core.exception.FlowException;
import com.czy.workflow.core.model.TaskInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Created by admin on 2019/8/31.
 */


@TaskExecutor(flowCode = "F1")
public class WorkDemo1 {

    Logger logger = Logger.getLogger(WorkDemo1.class.getName());

    @Autowired
    FlowContext flowContext;

    @TaskExecutorMethod(nodeCode = "N1")
    public boolean handleNote1(TaskInfo taskInfo){
        logger.info("----F1---N1----");
        logger.info(JSONUtil.toJsonStr(taskInfo.getNode()));
        Map<String, Integer> params = taskInfo.getParams();
        if(params == null){
            params = new HashMap<>();
            params.put("sum", 10);
        }
        params.put("sum", params.get("sum") + 1);
        return true;
    }

    @TaskExecutorMethod(nodeCode = "N2")
    public boolean handleNote2(TaskInfo taskInfo){
        logger.info("----F1---N2----");
        logger.info(JSONUtil.toJsonStr(taskInfo.getNode()));
        Map<String, Integer> params = taskInfo.getParams();
        if(params == null){
            params = new HashMap<>();
            params.put("sum", 10);
        }
        logger.info(String.format("sum = %d", params.get("sum")));
        return true;
    }

    public void startDemo(){
        TaskInfo taskInfo = new TaskInfo();
        taskInfo.setDocId("1");
        taskInfo.setFlowId("1");
        Map<String, Integer> params = new HashMap<>();
        params.put("sum", 0);
        taskInfo.setParams(params);
        try {
            flowContext.getTaskExecutor().startTask(taskInfo);
        } catch (FlowException e) {
            e.printStackTrace();
        }
    }


}
